import React, { useCallback } from "react"
import { Field_FormSave, Field_name_id } from "@/components/utils/field.list"
import { FormProps } from "@/components/modal/form.modal"
import { FileExtMeta, FormRender, FormUpload } from "@whzhuke/antd"
import useForm from "antd/es/form/hooks/useForm"
import baseSystem from "@/services/base.system"
import contractService from "@/services/contract.service"

interface Props {
  values?: any
}
export const EditContracts: React.FC<Props & FormProps> = props => {
  const { values, onDone } = props
  const [form] = useForm<any>()
  const onFinish = useCallback(
    data => {
      contractService[values ? "edit" : "add"]({ ...data }).then(res => {
        onDone?.()
      })
    },
    [values]
  )

  return (
    <FormRender
      form={form}
      onFinish={onFinish}
      initialValues={
        values
          ? {
              ...values
            }
          : {}
      }
      fieldList={[
        values ? Field_name_id : undefined,
        {
          fieldType: "input",
          formItemConfig: { required: !0, name: "code", label: "合同编号" },
          config: { placeholder: "合同编号" }
        },
        {
          fieldType: "input",
          formItemConfig: { required: !0, name: "name", label: "合同名称" },
          config: { placeholder: "合同名称" }
        },

        {
          fieldType: "diy",
          formItemConfig: {
            name: "fileUrl",
            label: "合同文件",
            required: !0,
            rules: [{ required: !0, message: "请上传产品文件" }],
            normalize: v => v[0]
          },
          component: (
            <FormUpload
              apiService={baseSystem.upload}
              accept={FileExtMeta.document.toString()}
              allowFile="document"
              assetKey="public"
              apiParams={{ businessType: 5 }}
            />
          )
        },
        { ...Field_FormSave, formItemConfig: { wrapperCol: { offset: 8, span: 16 } } }
      ]}
      autoComplete="off"
    />
  )
}
